/*  */
#include <iostream>
#include <fstream>
#include <log4cpp/Appender.hh>
#include <log4cpp/Category.hh>
#include <log4cpp/OstreamAppender.hh>
#include <log4cpp/FileAppender.hh>
#include <log4cpp/RollingFileAppender.hh>
#include <log4cpp/Layout.hh>
#include <log4cpp/BasicLayout.hh>
#include <log4cpp/PatternLayout.hh>
#include <log4cpp/Priority.hh>
using std::cout;
using std::endl;
using std::ofstream;
using namespace log4cpp;
int main()
{
    //控制台输出器
    //new一个输出器
    OstreamAppender * appender1 = new OstreamAppender("console", &cout);
    //new一个格式器
    PatternLayout * layout1 = new PatternLayout();
    //自定义格式器
    layout1->setConversionPattern("%c %d [%p] %m%n");
    //输出器绑定格式器
    appender1->setLayout(layout1);


    //文件输出器
    //new一个输出器
    FileAppender * appender2 = new FileAppender("file", "file.log");
    //new一个格式器
    PatternLayout * layout2 = new PatternLayout();
    //自定义格式器
    layout2->setConversionPattern("%c %d [%p] %m%n");
    //输出器绑定格式器
    appender2->setLayout(layout2);


    //回卷文件输出器
    //new一个输出器
    RollingFileAppender * appender3 = new RollingFileAppender("rolling", "roll.log", 100, 3);
    //new一个格式器
    PatternLayout * layout3 = new PatternLayout();
    //自定义格式器
    layout3->setConversionPattern("%c %d [%p] %m%n");
    //输出器绑定格式器
    appender3->setLayout(layout3);

    //记录器
    Category & root = Category::getRoot();
    //设置优先级(添加过滤器),记录器绑定输出器
    //root appender1 INFO 
    root.setPriority(Priority::INFO);
    root.addAppender(appender1);
    //sub1 appender1 appender2 INFO
    Category & sub1 = Category::getInstance("sub1");
    sub1.addAppender(appender2);
    //sub2 appender3 DEBUG
    Category & sub2 = Category::getInstance("sub2");
    sub2.setAdditivity(false);
    sub2.setPriority(Priority::DEBUG);
    sub2.addAppender(appender3);

    root.info("info");
    root.debug("debug");
    sub1.info("info");
    sub1.debug("debug");
    for (int i = 0; i < 100; i++) {
        sub2.info("info");
        sub2.debug("debug");
    }
    Category::shutdown();


    return 0;
}

